package com.taru.controller;


import com.taru.model.User;
import com.taru.service.UserService;
import com.taru.utils.Constant;
import com.taru.utils.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@CrossOrigin
public class UserController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    @RequestMapping("/query/open/id")
    @ResponseBody
    public Object queryUserByOpenId(String openId){
        JsonResult result ;
        try {
            User user= userService.queryUserByOpenId(openId);
            logger.info("微信唯一标识Id：{}", openId);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS, user);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/query/user/audit")
    @ResponseBody
    public Object queryUserInAudit(){
        JsonResult result ;
        try {
            List<User> userList= userService.queryUserInAudit();
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS, userList);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }


    @RequestMapping("/insert/user")
    @ResponseBody
    public Object insertUser(User users){
        JsonResult result = null;
        try {
           int i = userService.insertUser(users);
            if(i == 0){
                logger.info("添加", users.toString());
                result = new JsonResult("添加失败", Constant.STATUC_FAILURE);
            }else if(i == 1){
                logger.info("添加", users.toString());
                result = new JsonResult("添加成功", Constant.STATUC_SUCCESS);
            }
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("注册异常", Constant.STATUC_FAILURE);
        }
        return result;
    }


    @RequestMapping("/update/user/id")
    @ResponseBody
    public Object updateUserByOpenId(User users){
        JsonResult result = null;
        try {
           int i = userService.updateUserByOpenId(users);
            if(i == 0){
                logger.info("修改", users.toString());
                result = new JsonResult("修改失败", Constant.STATUC_FAILURE);
            }else if(i == 1){
                logger.info("添加", users.toString());
                result = new JsonResult("修改成功", Constant.STATUC_SUCCESS);
            }
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("更新异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

}
